package com.example.tao.mycar.login;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.tao.mycar.MyActionBarActivity;
import com.example.tao.mycar.MyApplication;
import com.example.tao.mycar.R;
import com.example.tao.mycar.utils.RequestUtils;

public class SetBabySexActivity extends MyActionBarActivity implements View.OnClickListener{

    private Button nextButton;
    private ImageView boyImg, girlImg;

    private String babySex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_set_baby_sex);

        nextButton = (Button)this.findViewById(R.id.next_step);
        boyImg = (ImageView)this.findViewById(R.id.boy);
        girlImg = (ImageView)this.findViewById(R.id.girl);

        nextButton.setOnClickListener(this);
        boyImg.setOnClickListener(this);
        girlImg.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.next_step :
                if (TextUtils.isEmpty(babySex)) {
                    Toast.makeText(SetBabySexActivity.this, "是男孩还是女孩？",Toast.LENGTH_LONG).show();
                    break;
                }
                RequestUtils.getSharedPreferenceEditor(this).putString(RequestUtils.BABYSEX, babySex).commit();
                Intent intent = new Intent(SetBabySexActivity.this, SetBabyBirthdayActivity.class);
                startActivity(intent);
                break;
            case R.id.boy :
                setSelect(boyImg);
                break;
            case R.id.girl :
                setSelect(girlImg);
                break;
            default:
                break;
        }
    }

    public void setSelect(ImageView imageView) {
        Bitmap bitmapSelect = ((BitmapDrawable)getResources().getDrawable(R.mipmap.check_select2x)).getBitmap();
        Bitmap bitmapImage;
        if (imageView == boyImg) {
            bitmapImage = ((BitmapDrawable)getResources().getDrawable(R.mipmap.boy2x)).getBitmap();
            babySex = "小王子";
            setBitmap(imageView, bitmapImage, bitmapSelect);
            girlImg.setImageResource(R.mipmap.girl2x);
        } else if (imageView == girlImg) {
            bitmapImage = ((BitmapDrawable)getResources().getDrawable(R.mipmap.girl2x)).getBitmap();
            babySex = "小公举";
            setBitmap(imageView, bitmapImage, bitmapSelect);
            boyImg.setImageResource(R.mipmap.boy2x);
        }
    }

    private void setBitmap(ImageView imageView, Bitmap bitmapImage, Bitmap bitmapSelect) {
        Drawable[] array = new Drawable[2];
        array[0] = new BitmapDrawable(bitmapImage);
        array[1] = new BitmapDrawable(bitmapSelect);
        LayerDrawable layerDrawable = new LayerDrawable(array);
        layerDrawable.setLayerInset(0, 0, 0, 0, 0);
        layerDrawable.setLayerInset(1, 75, 75, 0, 0);
        imageView.setImageDrawable(layerDrawable);
    }
}
